import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ResourceHogs extends JFrame implements WindowListener {
	
	private static final long serialVersionUID = 7526472295622776147L;
	private static int DEFAULT_FPS = 85;
	
	// Acquiring the current Graphics Device and Graphics Configuration
	GraphicsEnvironment graphEnv;
	GraphicsDevice graphDevice;
	GraphicsConfiguration graphicConf;
	
	GamePanel gamePanel; 

	DisplayMode originalMode;
	boolean isFullScreen;

    
	public ResourceHogs(GraphicsDevice graphDevice, long period) {
		super(graphDevice.getDefaultConfiguration());

		this.graphDevice = graphDevice;
		originalMode = graphDevice.getDisplayMode();
		System.out.println("Display Mode = " + originalMode.getWidth() + "x" + originalMode.getHeight() + "x" + originalMode.getBitDepth() + "@" + originalMode.getRefreshRate() + "Hz" );

		this.setIgnoreRepaint(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setFocusTraversalKeysEnabled(false);
		
	    // make gui here
		makeGUI(period);

	    addWindowListener( this );
    }
	
	private void makeGUI(long period)
	{
		Container c = getContentPane();    // default BorderLayout used

		gamePanel = new GamePanel(this, period);
		c.add(gamePanel, "Center");
	}

    public void initScreen() {
        isFullScreen = graphDevice.isFullScreenSupported();
        setUndecorated(isFullScreen);
        setResizable(!isFullScreen);
        
        if (isFullScreen) {
            // Full-screen mode
        	System.out.println("Full Screen mode!");
            graphDevice.setFullScreenWindow(this);
    		if (graphDevice.isDisplayChangeSupported()) {
    			graphDevice.setDisplayMode(new DisplayMode(640, 480, 32, DisplayMode.REFRESH_RATE_UNKNOWN));
    		}            
            validate();
        } else {
            // Windowed mode
            pack();
            setVisible(true);
        }
    }
    
    public void start() {
    	gamePanel.startGame();
    }
    
    public void stop() {
    	//System.out.println("Original Display Mode");
    	//graphDevice.setDisplayMode(originalMode);
    }
	
    // ----------------- window listener methods -------------
    public void windowActivated(WindowEvent e) {
    	gamePanel.resumeGame();
    }

    public void windowDeactivated(WindowEvent e) {
    	gamePanel.pauseGame();
    }

    public void windowDeiconified(WindowEvent e) {
    	gamePanel.resumeGame();
    }

    public void windowIconified(WindowEvent e) {
    	gamePanel.pauseGame();
    }

    public void windowClosing(WindowEvent e) {
    	gamePanel.stopGame();
    }

    public void windowClosed(WindowEvent e) {}
    public void windowOpened(WindowEvent e) {}
    
    public static void main(String args[])
    { 
    	// Acquiring the current Graphics Device and Graphics Configuration
    	GraphicsEnvironment graphEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    	GraphicsDevice graphDevice = graphEnv.getDefaultScreenDevice();
    	
    	int fps = DEFAULT_FPS;
    	if (args.length != 0)
    		fps = Integer.parseInt(args[0]);

    	int period = (int) 1000.0/fps;
    	System.out.println("fps: " + fps + "; period: " + period + " ms");

    	ResourceHogs rh = new ResourceHogs(graphDevice, period*1000000L); // ms to nanosecs
    	rh.initScreen();
    	rh.start();
    }
}
